package com.calvin.lingting.actions;

import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.calvin.lib.CActivity;
import com.calvin.lib.EventDispatch;
import com.calvin.lib.EventDispatch.CListen;
import com.calvin.lib.annotation.ViewInject;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lib.utils.CUtils;
import com.calvin.lingting.MainActivity;
import com.calvin.lingting.R;
import com.calvin.lingting.config.NTConfig;

public class MainAction {
    int mTitleSelected = 0;
    LinearLayout mNavTitleGroup;
    ViewPager mViewPager;

    @ViewInject(isTag = true, tag = "nav_left_button")
    public void onNavLeftClick(View view) {
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT,R.layout.action_setting,new SettingAction());
    }

    @ViewInject(isTag = true, tag = "nav_right_button")
    public void onNavRightClick(View view) {
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT, R.layout.action_search, new SearchAction());
    }

    public void onTitleClick(View view) {
        if ((Integer) view.getTag() > 0) {
            Toast.makeText(MainActivity.sharedCurrent(), R.string.s_wait_develop, Toast.LENGTH_SHORT).show();
        }
    }

    @ViewInject(isTag = true, tag = {"nav_title_group", "nav_left_button", "nav_right_button", "page_view"}, listen = CListen.INIT)
    public void onInit(View view) {
        if ("nav_title_group".equals(view.getTag())) {
            mNavTitleGroup = (LinearLayout) view;
            String[] titles = MainActivity.sharedCurrent().getResources().getStringArray(R.array.a_main_titles);
            for (int i = 0; i < titles.length; i++) {
                View.inflate(MainActivity.sharedCurrent(), R.layout.layout_title_item, mNavTitleGroup);
                TextView textView = CUtils.cast(mNavTitleGroup.getChildAt(i), TextView.class);
                textView.setText(titles[i]);
                textView.setTag(i);
                EventDispatch.shared().put(textView, CListen.CLICK, this, "onTitleClick");
            }
            CUtils.cast(mNavTitleGroup.getChildAt(mTitleSelected), TextView.class).setTextColor(Color.WHITE);
        } else if ("nav_left_button".equals(view.getTag())) {
            view.setBackgroundResource(R.drawable.iconfont_menu);
        } else if ("nav_right_button".equals(view.getTag())) {
            view.setBackgroundResource(R.drawable.iconfont_search);
        } else if ("page_view".equals(view.getTag())) {
            mViewPager = (ViewPager) view;
            mViewPager.setAdapter(new ViewPagerAdapter());
            mViewPager.setOnPageChangeListener(new PageChangeListener());
        }
    }

    class ViewPagerAdapter extends PagerAdapter {
        View[] mViews;

        public ViewPagerAdapter() {
            mViews = new View[1];
            mViews[0] = View.inflate(MainActivity.sharedCurrent(), R.layout.action_zone, null);
            CActivity.initViewInject(new ZoneAction(), mViews[0]);
        }

        @Override
        public int getCount() {
            return mViews.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViews[position]);
            return mViews[position];
        }
    }

    class PageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageSelected(int arg0) {
            onTitleClick(mNavTitleGroup.getChildAt(arg0));
        }

    }
}
